// vue3中创建store实例对象的方法createStore()按需引入
import { createStore } from 'vuex'
import {login,getUserInfo} from "@/api/manager.js";
import {  tokenCookie } from "@/composables/utils.js"
const { setToken, removeToken } =tokenCookie()
export default createStore({
  state() {
    return {
        user:{},
        asideWidth: '250px'
    }
  },
  mutations: {
    SET_USER_INFO(state, user) {
      state.user = user
    },
    SET_ASIDE_WIDTH(state) {
      state.asideWidth = state.asideWidth === '250px' ? '64px' : '250px'
    }
  },
  actions: {
    login({ commit }, {  username, password }) {
      return new Promise((resolve, reject) => {
        login(username, password).then(res => {
            setToken(res.token)
             resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    getUserInfo({ commit }) {
      return new Promise((resolve, reject) => {
        // 这里可以根据token获取用户信息
          getUserInfo().then(res => {
            commit('SET_USER_INFO', res)
            resolve(res)
      }).catch(err =>  reject(err) )

    })
  },
  //   退出登录
    logout({ commit }) {
      removeToken()
      commit('SET_USER_INFO', {})
    }
  // getters: {
  // },
  // modules: {
  // }
  }
})

